﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.ComponentModel.DataAnnotations;

using ComLib;
using ComLib.Web.Lib.Attributes;
using ComLib.Web.Modules.Widgets;


namespace ComLib.CMS.Models.Widgets
{

    /// <summary>
    /// BlogRoll Widget
    /// </summary>
    [Widget(Name = "Image", IsCachable = true, IsEditable = true, SortIndex = 5)]
    public class Image : WidgetInstance
    {

        /// <summary>
        /// The full address in one line separated by comma to represent the address the show.
        /// </summary>
        [PropertyDisplay(Order = 1, DefaultValue = "", Description = "Title for the image")]
        public string Title { get; set; }


        /// <summary>
        /// Some textual content for the map supplied by user.
        /// </summary>
        [PropertyDisplay(Order = 2, DefaultValue = "", Description = "Optional text to display as a caption for image.")]
        public string Caption { get; set; }


        /// <summary>
        /// Height of the map
        /// </summary>
        [Range(0, 800)]
        [PropertyDisplay(Order = 4, DefaultValue = "120", Description = "The height of the image")]
        public int Height { get; set; }


        /// <summary>
        /// Width of the map
        /// </summary>
        [Range(0, 800)]
        [PropertyDisplay(Order = 5, DefaultValue = "120", Description = "The width of the image")]
        public int Width { get; set; }


        /// <summary>
        /// Whether or not to use pixels or height for dimensions.
        /// </summary>
        [PropertyDisplay(Order = 7, Description = "Whether or not to use pixels rather than percent dimensions")]
        public bool UsePixelDimensions { get; set; }



        /// <summary>
        /// Some textual content for the map supplied by user.
        /// </summary>
        [PropertyDisplay(Order = 8, DefaultValue = "", Description = "How to align the image ( top bottom middle left right)")]
        public string Alignment { get; set; }


        /// <summary>
        /// Some textual content for the map supplied by user.
        /// </summary>
        [PropertyDisplay(Order = 9, DefaultValue = "", Description = "Url of the image")]
        public string ExternalUrl { get; set; }


        /// <summary>
        /// Whether or not this is self-renderable.
        /// </summary>
        public override bool IsSelfRenderable
        {
            get { return true; }
        }


        /// <summary>
        /// Renders the html for the widget.
        /// </summary>
        /// <returns></returns>
        public override string DoRender()
        {
            var buffer = new StringBuilder();

            // Title ?
            if (!string.IsNullOrEmpty(Title))
                buffer.Append("<div>" + Title + "</div>");

            // Configure dimensions
            string dimensions = "";
            string dimensionMode = UsePixelDimensions ? "" : "%";

            if(Width == 0 && Height == 0)
                dimensions = "";
            else if(Width != 0 && Height != 0 )
                dimensions = " width=\"" + Width + dimensionMode + "\" height=\"" + Height + dimensionMode + "\" ";
            else if(Width != 0 && Height == 0)
                dimensions = " width=\"" + Width + dimensionMode + "\" ";
            else if (Height != 0 && Width == 0)
                dimensions = " height=\"" + Height + dimensionMode + "\" ";

            // Image
            string pattern = "<img src=\"{0}\" alt=\"{1}\"" + dimensions + "/>";
            string imgHtml = string.Format(pattern, ExternalUrl, Title);
            buffer.Append(imgHtml);

            // Add the caption.
            if (!string.IsNullOrEmpty(Caption))
                buffer.Append("<div>" + Caption + "</div>");

            return buffer.ToString();
        }
    }
}
